package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import dao.GoodsDao;
import dao.impl.GoodsDaoImpl;
import entity.Goods;
import services.GoodsService;
import services.impl.GoodsServiceImpl;


@WebServlet("/fukuan")
public class FukkuanServlet extends HttpServlet {
	
	GoodsService goodsService = new GoodsServiceImpl();
	Gson gson = new Gson();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String goods_name = request.getParameter("hidgoods");
		List<Goods> goodslist = goodsService.getGoodsByName(goods_name);
		String data = null;
		if(goodslist!=null) {
			Goods goods = goodslist.get(0);
			String strgoods = gson.toJson(goods);
			data = strgoods;
			String goodsdata = gson.toJson(data);
			request.setAttribute("goods", goodsdata);
			request.getRequestDispatcher("/fukuan.jsp").forward(request, response);
		}else {
			out.print("商品下架了!!");
			response.sendRedirect("index.jsp");
		}
		
		out.flush();
		out.close();
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
